package com.springseries.websocket.config;

import com.springseries.websocket.common.Constants;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    /**
     * 注册一个websocket的stomp，页面就可以发现该服务
     * @param registry
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint(Constants.WEBSOCKET_PATH).withSockJS();
    }

    /**
     * 配置一发送域等信息
     * @param registry
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        //服务端发送消息给客户端的域,多个用逗号隔开
        registry.enableSimpleBroker(Constants.WEBSOCKET_PATH,Constants.P2P_PUSH_BASE_PATH);
        //定义一对一推送的时候前缀
        registry.setUserDestinationPrefix(Constants.P2P_PUSH_BASE_PATH);
        //定义websocket前缀
        registry.setApplicationDestinationPrefixes(Constants.WEBSOCKET_PATH_PREFIX);
    }
}
